// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司 
// SPDX-License-Identifier: GPL-3.0-only 

import { page } from "$app/state";
import { list_my as list_my_board, get as get_board, type BoardInfo } from "$lib/api/board/board";
import { get_unread_count } from "$lib/api/board/board_chat";
import { list_my as list_my_member, get as get_member, type MemberInfo } from "$lib/api/board/board_member";
import { request } from "$lib/utils/request";
import { userStore } from "./user.svelte";

export class WebBoardInfo {
    public boardInfo: BoardInfo | null = $state(null);
    public myMember: MemberInfo | null = $state(null);
    public unReadCount = $state(0);
}

export class BoardStore {
    public webBoardList = $state([] as WebBoardInfo[]);
    public curWebBoard = $derived(this.webBoardList.find(item => item.boardInfo?.board_id == page.url.searchParams.get("boardId")));
    public curBoardId = $derived(page.url.searchParams.get("boardId") ?? "");
    public drawVersion = $state(1);

    async init() {
        if (userStore.userSession == "") {
            this.webBoardList = [];
            return;
        }
        this.loadWebBoardList();
    }

    async loadWebBoardList() {
        if (userStore.userSession == "") {
            return;
        }
        const boardRes = await request(list_my_board({
            session_id: userStore.userSession,
        }));

        const memberRes = await request(list_my_member({
            session_id: userStore.userSession,
        }));
        const tmpList = [] as WebBoardInfo[];
        for (const board of boardRes.board_list) {
            const member = memberRes.member_list.find(item => item.board_id == board.board_id);
            const readStateRes = await request(get_unread_count({
                session_id: userStore.userSession,
                board_id: board.board_id,
                draw_id: "",
            }));
            tmpList.push({
                boardInfo: board,
                myMember: member ?? null,
                unReadCount: readStateRes.unread_count,
            });
        }
        tmpList.sort((a, b) => (b.boardInfo?.update_time ?? 0) - (a.boardInfo?.update_time ?? 0));
        this.webBoardList = tmpList;
    }

    async onJoinBoard(boardId: string) {
        const tmpList = this.webBoardList.slice();
        const index = tmpList.findIndex(item => item.boardInfo?.board_id == boardId);
        if (index != -1) {
            return;
        }
        const boardRes = await request(get_board({
            session_id: userStore.userSession,
            board_id: boardId,
        }));
        const memberRes = await request(get_member({
            session_id: userStore.userSession,
            board_id: boardId,
            member_user_id: userStore.userInfo?.user_id ?? "",
        }));
        const readStateRes = await request(get_unread_count({
            session_id: userStore.userSession,
            board_id: boardId,
            draw_id: "",
        }));
        tmpList.unshift({
            boardInfo: boardRes.board,
            myMember: memberRes.member,
            unReadCount: readStateRes.unread_count,
        });
        tmpList.sort((a, b) => (b.boardInfo?.update_time ?? 0) - (a.boardInfo?.update_time ?? 0));
        this.webBoardList = tmpList;
    }

    async onUpdateBoard(boardId: string) {
        const tmpList = this.webBoardList.slice();
        const index = tmpList.findIndex(item => item.boardInfo?.board_id == boardId);
        if (index == -1) {
            return;
        }
        const boardRes = await request(get_board({
            session_id: userStore.userSession,
            board_id: boardId,
        }));
        tmpList[index].boardInfo = boardRes.board;
        tmpList.sort((a, b) => (b.boardInfo?.update_time ?? 0) - (a.boardInfo?.update_time ?? 0));
        this.webBoardList = tmpList;
    }

    async onUpdateMyPerm(boardId: string) {
        const tmpList = this.webBoardList.slice();
        const index = tmpList.findIndex(item => item.boardInfo?.board_id == boardId);
        if (index == -1) {
            return;
        }
        const memberRes = await request(get_member({
            session_id: userStore.userSession,
            board_id: boardId,
            member_user_id: userStore.userInfo?.user_id ?? "",
        }));
        tmpList[index].myMember = memberRes.member;
        this.webBoardList = tmpList;
    }

    onRemoveBoard(boardId: string) {
        const tmpList = this.webBoardList.filter(item => item.boardInfo?.board_id != boardId);
        this.webBoardList = tmpList;
    }

    async onUpdateUnreadCount(boardId: string) {
        const tmpList = this.webBoardList.slice();
        const index = tmpList.findIndex(item => item.boardInfo?.board_id == boardId);
        if (index == -1) {
            return;
        }
        const res = await request(get_unread_count({
            session_id: userStore.userSession,
            board_id: boardId,
            draw_id: "",
        }));
        tmpList[index].unReadCount = res.unread_count;
        this.webBoardList = tmpList;
    }
}

export const boardStore = $state(new BoardStore());